package com.cal.calendar;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class MonthGridViewAdapter extends BaseAdapter {

	// 记录当前月的信息
	private Calendar currentMonth = Calendar.getInstance();

	// 用户点击选择的月
	private Calendar selectMonth = Calendar.getInstance();

	// 记录今天的数据
	private Calendar today = Calendar.getInstance();

	// 记录当前选择的月份的下标
	private int currentMonthView = 0;

	// 日历显示的当前月的所有日期
	private List<Date> currentMonthDaysList;

	// 容器运行类
	private Activity activity;

	public MonthGridViewAdapter(Activity activity) {
		this.activity = activity;
	}
	
	public MonthGridViewAdapter(Activity activity,Calendar currentMonth) {
		this.activity = activity;
		this.currentMonth = currentMonth;
		this.currentMonthDaysList = getCurrentMonthShowDays();
	}

	/**
	 * 将当前日历调整到当前月1号，所在周的上周日的位置
	 */
	public void updateMonthFromStart() {
		// 将日历设置成当前月的第一天
		currentMonth.set(Calendar.DAY_OF_MONTH, 1);
		// 保存当前月的下标
		currentMonthView = currentMonth.get(Calendar.MONTH);

		// 获取当前天在当前周中的下标位置,周天为1,以此类推,周六为7
		int offsetDay = currentMonth.get(Calendar.DAY_OF_WEEK)
				- Calendar.SUNDAY;

		if (offsetDay < 0) {
			offsetDay = 6;
		}
		// 将日历调整到当前周的开始位置
		currentMonth.add(Calendar.DAY_OF_WEEK, 0 - offsetDay);
	}

	/**
	 * 获取日历当前显示的所有日期
	 * 
	 * @return 42个日期值
	 */
	public List<Date> getCurrentMonthShowDays() {
		
		updateMonthFromStart();

		List<Date> daysList = new ArrayList<Date>();

		for (int i = 0; i < 42; i++) {
			daysList.add(currentMonth.getTime());
			currentMonth.add(Calendar.DATE, 1);
		}
		return daysList;
	}

	@Override
	public int getCount() {

		return currentMonthDaysList.size();
	}

	@Override
	public Object getItem(int position) {

		return currentMonthDaysList.get(position);
	}

	@Override
	public long getItemId(int position) {

		return position;
	}
	
	private String getLunarContent(Lunar lunar){
		String result = "";
		if("初一".equals(lunar.getDay())){
			result = lunar.getMonth();
		}else{
			result = lunar.getDay();
		}
		return result;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		// 创建一个单元格
		LinearLayout dateCell = new LinearLayout(activity);
		// 设置单元格的属性
		dateCell.setId(position + 2000);
		dateCell.setGravity(Gravity.CENTER);
		dateCell.setOrientation(LinearLayout.VERTICAL);
		dateCell.setBackgroundColor(0xD8D7DC);

		// 创建单元格显示的内容下半部分
		TextView downTextShow = new TextView(activity);
		downTextShow.setGravity(Gravity.CENTER_HORIZONTAL);
		downTextShow.setTextSize(9);

		Date currentDate = (Date) getItem(position);
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(currentDate);
		Lunar lunar = new Lunar(cal);
		
		// 如果是今天
		if (DateUtil.equalsTwoDate(currentDate, today.getTime())) {
			dateCell.setBackgroundColor(Color.BLUE);
			downTextShow.setText(getLunarContent(lunar));
			downTextShow.setTextColor(Color.GRAY);
		}else{
			downTextShow.setText(getLunarContent(lunar));
			downTextShow.setTextColor(Color.GRAY);
		}

		// 如果是用户选择的单元格
		if (DateUtil.equalsTwoDate(currentDate, selectMonth.getTime())) {
			dateCell.setBackgroundColor(Color.CYAN);
		}

		// 创建单元格显示的内容上半部分
		TextView upTextShow = new TextView(activity);
		upTextShow.setId(position + 1000);
		upTextShow.setGravity(Gravity.CENTER_HORIZONTAL);
		upTextShow.setText(String.valueOf(currentDate.getDate()));
		upTextShow.setTextSize(16);

		// 如果单元格显示的时间的月份和当前月一致
		if (currentDate.getMonth() == currentMonthView) {
			upTextShow.setTextColor(Color.rgb(50, 50, 50));
			downTextShow.setTextColor(Color.rgb(50, 50, 50));
		} else {
			upTextShow.setTextColor(Color.rgb(150, 150, 150));
			downTextShow.setTextColor(Color.rgb(150, 150, 150));
		}

		// 添加内容的显示方式
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
				LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		
		dateCell.addView(upTextShow, params);
		dateCell.addView(downTextShow, params);
		
		dateCell.setTag(currentDate);

		return dateCell;
	}

	// setter and getter
	public void setSelectMonth(Calendar selectMonth) {
		this.selectMonth = selectMonth;
	}

}
